﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Looptribe.Moodlin8.Model;
using Looptribe.Moodlin8.ViewModel;
using Looptribe.WinRT.Toolkit.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Looptribe.Moodlin8.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class HubPage : Looptribe.Moodlin8.Common.LayoutAwarePage
    {
        private HubViewModel ViewModel { get { return (HubViewModel)DataContext; } }

        public HubPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            ViewModel.LoadContent();
        }

        private void ScrollViewer_Loaded(object sender, RoutedEventArgs e)
        {
            // Make Mouse wheel works over all inner listviews to make horizonal scorlling.
            hubEnrolledCourseGridview.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(BubblePointerWheelChanged), true);
            hubNextAssignmentsListview.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(BubblePointerWheelChanged), true);
            hubCorrectedAssignmentsListview.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(BubblePointerWheelChanged), true);
            hubUploadsListview.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(BubblePointerWheelChanged), true);
            hubMessagesListview.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(BubblePointerWheelChanged), true);
        }

        private void BubblePointerWheelChanged(object sender, PointerRoutedEventArgs e)
        {
            outerScrollViewer.ScrollToHorizontalOffset(outerScrollViewer.HorizontalOffset - e.GetCurrentPoint(null).Properties.MouseWheelDelta);
        }

        private void DisplayMonthList()
        {
            if (!MonthPopup.IsOpen)
                MonthPopup.IsOpen = true;
        }

        private void MonthTitleClicked(object sender, PointerRoutedEventArgs e)
        {
            DisplayMonthList();
        }

        private void MonthTitleButtonClicked(object sender, RoutedEventArgs e)
        {
            DisplayMonthList();
        }

        private void MonthSelectionChanges(object sender, SelectionChangedEventArgs e)
        {
            var lb = (ListBox) sender;
            ViewModel.CurrentMonth = ViewModel.CalendarMonths[lb.SelectedIndex];
            MonthPopup.IsOpen = false;
        }

        private void CourseGrid_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.ClickedItem != null)
            {
                var course = ((CoursesCourseWrapper) e.ClickedItem).Course;
                if (ViewModel.GotoCourseCommand.CanExecute(course))
                    ViewModel.GotoCourseCommand.Execute(course);
            }
        }

        private void CalendarDayTapped(object sender, CalendarDayEventArgs e)
        {
            if (ViewModel.GotoDateCommand.CanExecute(e.Date))
                ViewModel.GotoDateCommand.Execute(e.Date);
        }

        private void OpenUploadClick(object sender, RoutedEventArgs e)
        {
            var file = (FileWrapper) ((FrameworkElement) sender).DataContext;
            if (ViewModel.OpenUploadCommand.CanExecute(file))
                ViewModel.OpenUploadCommand.Execute(file);
        }

        private void AssignmentItemClick(object sender, ItemClickEventArgs e)
        {
            var assignment = e.ClickedItem as AssignmentWrapper;
            if (ViewModel.GotoAssignmentCommand.CanExecute(assignment))
                ViewModel.GotoAssignmentCommand.Execute(assignment);
        }

        private void MessagesItemClick(object sender, ItemClickEventArgs e)
        {
            var msg = e.ClickedItem as MessageWrapper;
            if (msg != null && ViewModel.GotoUserMessagesCommand.CanExecute(msg.Sender.WrappedObject))
                ViewModel.GotoUserMessagesCommand.Execute(msg.Sender.WrappedObject);
        }
    }
}
